package cn.bincker.mybatis.encrypt.converter.impl;

import cn.bincker.mybatis.encrypt.exception.InvalidDataException;

import java.nio.ByteBuffer;

public class IntegerEncryptConverter extends BaseEncryptConverter<Integer> {
    @Override
    public Integer convertNonNull(byte[] data) {
        if (data.length >= Integer.BYTES) {
            return ByteBuffer.wrap(data).getInt();
        }else if(data.length >= Short.BYTES){
            return (int) ByteBuffer.wrap(data).getShort();
        }else throw new InvalidDataException("integer need " + Integer.BYTES + " bytes but got " + data.length);
    }

    @Override
    public byte[] convertNonNull(Integer object) {
        byte[] result = new byte[Integer.BYTES];
        ByteBuffer.wrap(result).putInt(object);
        return result;
    }
}
